package com.travel.server.manager.impl.base;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.server.manager.model.UserInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\11 0011 20:40
 * @Desc:
 */
public class BaseCommand {

    public HttpServletRequest getHttpServletRequest(){
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return httpServletRequest;
    }

    public HttpServletResponse getHttpServletResponse(){
        HttpServletResponse httpServletResponse= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return httpServletResponse;
    }

    public UserInfo getUserInfo(){
        HttpServletRequest request = getHttpServletRequest();
        Object userObj = request.getSession().getAttribute(CommonConstant.SESSION_USER_INFO);
        if(userObj == null){
            throw new WorkException(WorkCode.LOGIN_TIME_OUT);
        }
        return (UserInfo)userObj;
    }
}
